
#include "mainwindowtitle.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QPushButton>
#include <QImage>
#include <QtSvg/QSvgRenderer>
#include <QApplication>
#include <QFile>

MainWindowTitle::MainWindowTitle(QWidget *parent)
    : QWidget(parent)
{
    initUi();
    setFocusPolicy(Qt::NoFocus);
}

MainWindowTitle::~MainWindowTitle()
{

}

void MainWindowTitle::initUi()
{
    QWidget *widget = new QWidget(this);
    widget->setObjectName("window_title");
    auto *layout1 = new QHBoxLayout(this);
    layout1->setMargin(0);
    layout1->addWidget(widget);

    icon_title_ = (new QLabel(widget));
    icon_setting_ = (new QPushButton(widget));
    icon_setting_->setObjectName("btn_setting");
    icon_min_ = (new QPushButton(widget));
    icon_min_->setObjectName("btn_min");
    icon_max_ = (new QPushButton(widget));
    icon_max_->setObjectName("btn_max");
    icon_close_ = (new QPushButton(widget));
    icon_close_->setObjectName("btn_close");

    auto *layout = new QHBoxLayout(widget);
    layout->setContentsMargins(6,0, 6, 0);
    QSpacerItem *spacerItem = new QSpacerItem(40, 30, QSizePolicy::Expanding, QSizePolicy::Preferred);
    layout->addWidget(icon_title_);
    layout->addItem(spacerItem);
    layout->addWidget(icon_setting_);
    layout->addWidget(icon_min_);
    layout->addWidget(icon_max_);
    layout->addWidget(icon_close_);
    setFixedHeight(30);
    icon_close_->setFixedSize(25, 25);
    icon_min_->setFixedSize(25, 25);
    icon_max_->setFixedSize(25, 25);
    icon_setting_->setFixedSize(25, 25);
    icon_title_->setFixedSize(25, 25);

    connect(icon_close_, &QPushButton::clicked, [this]{
        emit sig_closeBtnClicked();
    });

    connect(icon_max_, &QPushButton::clicked, [this]{
        if (dynamic_cast<QWidget*>(this->parent()->parent())->isMaximized())
        {
            dynamic_cast<QWidget*>(this->parent()->parent())->showNormal();
            return;
        }
        dynamic_cast<QWidget*>(this->parent()->parent())->showMaximized();
    });

    connect(icon_min_, &QPushButton::clicked, [this]{
        dynamic_cast<QWidget*>(this->parent()->parent())->showMinimized();
    });
    connect(icon_setting_, &QPushButton::clicked, [this]{
        static bool is_dark = true;
        if (is_dark)
        {
            QFile file(":/resources/style/dark/style.css");
            file.open(QIODevice::ReadOnly);
            // qDebug() << file.readAll();
            qApp->setStyleSheet(file.readAll());
            is_dark = false;
        }
        else
        {
            QFile file(":/resources/style/normal/style.css");
            file.open(QIODevice::ReadOnly);
            // qDebug() << file.readAll();
            qApp->setStyleSheet(file.readAll());
            is_dark = true;
        }

    });

    // 加载 SVG 文件
    QSvgRenderer svgRenderer(QStringLiteral(":/resources/svg/close.svg"));
    // 创建一个 QPixmap，大小与 SVG 默认大小匹配
    QPixmap pixmap(25, 25);
    pixmap.fill(Qt::transparent);  // 设置背景透明
    // 使用 QPainter 将 SVG 渲染到 QPixmap
    QPainter painter(&pixmap);
    svgRenderer.render(&painter);
    icon_close_->setIcon(QIcon(pixmap));

    svgRenderer.load(QStringLiteral(":/resources/svg/max.svg"));
    pixmap.fill(Qt::transparent);  // 设置背景透明
    svgRenderer.render(&painter);
    icon_max_->setIcon(QIcon(pixmap));

    svgRenderer.load(QStringLiteral(":/resources/svg/min.svg"));
    pixmap.fill(Qt::transparent);  // 设置背景透明
    svgRenderer.render(&painter);
    icon_min_->setIcon(QIcon(pixmap));


    svgRenderer.load(QStringLiteral(":/resources/svg/turbo.svg"));
    pixmap.fill(Qt::transparent);  // 设置背景透明
    svgRenderer.render(&painter);
    icon_title_->setPixmap(pixmap);

    svgRenderer.load(QStringLiteral(":/resources/svg/setting.svg"));
    pixmap.fill(Qt::transparent);  // 设置背景透明
    svgRenderer.render(&painter);
    icon_setting_->setIcon(QIcon(pixmap));

}